#include "light.h"

light xdata bulb[16]; //灯泡的结构体数组

uchar count; //记录玩家操作的次数

uchar code light_state[2][32] = {
    {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
    {0xFF, 0xFF, 0xFF, 0x0F, 0x07, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x07, 0x0F, 0xFF, 0xFF,
     0xFF, 0xFF, 0xFF, 0xF0, 0xE0, 0xC0, 0x80, 0x80, 0x80, 0x80, 0x80, 0xC0, 0xE0, 0xF0, 0xFF, 0xFF}};

uchar code text_count[5][16] = {
    {0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xF1, 0xEE, 0xDF, 0xDF, 0xDF, 0xEE, 0xFF},
    {0xFF, 0xFF, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xE0, 0xDF, 0xDF, 0xDF, 0xDF, 0xE0, 0xFF},
    {0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0xFF, 0xFF, 0xE0, 0xDF, 0xDF, 0xDF, 0xEF, 0xC0, 0xDF},
    {0x7F, 0x7F, 0xFF, 0x7F, 0x7F, 0x7F, 0xFF, 0xFF, 0xDF, 0xC0, 0xDE, 0xFF, 0xFF, 0xDF, 0xC0, 0xDF},
    {0xFF, 0x7F, 0x7F, 0x1F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0xDF, 0xDF, 0xEF, 0xFF}};

uchar code text_win[7][16] = {
    {0x08, 0x38, 0xC8, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00},
    {0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00},
    {0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x10, 0x3F, 0x20},
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    {0x08, 0xF8, 0x00, 0xF8, 0x00, 0xF8, 0x08, 0x00, 0x00, 0x03, 0x3E, 0x01, 0x3E, 0x03, 0x00, 0x00},
    {0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00},
    {0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20}};

//初始化16盏灯
void bulb_initate()
{
    uchar i, j;

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            bulb[4 * i + j].x = j * 16;
            bulb[4 * i + j].y = i * 2;
            bulb[4 * i + j].state = 1; //默认灯泡全部点亮
        }
    }
    score_bit = 0; //初始化操作数显示情况
    score_ten = 0;
    count = 0; //初始化操作次数为0
}

//右屏打印初始信息 左屏点亮所有16盏灯
void bulb_meassage()
{
    uchar i;
    select_secreen(2);              //选择右屏
    print(0, 16, menu_text11, 16);  //"灭"
    print(0, 32, menu_text12, 16);  //"灯"
    print(2, 8, text_count[0], 8);  //字母c
    print(2, 16, text_count[1], 8); //字母o
    print(2, 24, text_count[2], 8); //字母u
    print(2, 32, text_count[3], 8); //字母n
    print(2, 40, text_count[4], 8); //字母t
    print(4, 8, number[0], 8);      //数字0  万位
    print(4, 16, number[0], 8);     //数字0  千位
    print(4, 24, number[0], 8);     //数字0  百位

    select_secreen(3); //选择左屏
    for (i = 0; i < 16; i++)
    {
        print(bulb[i].y, bulb[i].x, light_state[bulb[i].state], 16); //亮灯
    }

    //打印边界线
    for (i = 0; i < 8; i++)
    {
        select_secreen(3); //左屏打印
        set_row(i);        //每一页都打印
        set_column(63);
        write_data(0);
    }

}

//按下按键 对应按键的灯及其周围灯状态发生反转
void button_press(int x)
{
    bulb[x].state = 1-bulb[x].state;

    //改变上边灯泡的状态
    if (x - 4 >= 0)
    {
        bulb[x - 4].state = 1-bulb[x - 4].state;
    }

    //改变下边灯泡的状态
    if (x + 4 <= 15)
    {
        bulb[x + 4].state = 1-bulb[x + 4].state;
    }

    //改变左边灯泡的状态
    if (x % 4 != 0)
    {
        bulb[x - 1].state = 1-bulb[x - 1].state;
    }

    //改变右边灯泡的状态
    if ((x + 1) % 4 != 0)
    {
        bulb[x + 1].state = 1-bulb[x + 1].state;
    }
}

//显示刷新灯泡的
void bulb_print()
{
    uchar i;
    select_secreen(3); //选择左屏
    for (i = 0; i < 16; i++)
    {
        print(bulb[i].y, bulb[i].x, light_state[bulb[i].state], 16); //亮灯
    }
}

//游戏胜利条件
uchar button_over()
{
    uchar i, j;
    j = 0;
    for (i = 0; i < 16; i++)
    {
        j++;
        if (1 == bulb[i].state)
        {
            j = 0;
            break;
        }
    }
    if (16 == j)
    {

        return 0; //16盏灯全灭 游戏结束返回0
    }
    else
    {
        return 1; //还有灯没灭返回1；
    }
}

//灭灯游戏胜利打印相应的信息
void bulb_gameover()
{
    screen_clr2(3); //左屏清理

    select_secreen(3); //选择左屏打印

    //打印 You Win
    print(2, 0, text_win[0], 8);
    print(2, 8, text_win[1], 8);
    print(2, 16, text_win[2], 8);
    print(2, 24, text_win[3], 8);
    print(2, 32, text_win[4], 8);
    print(2, 40, text_win[5], 8);
    print(2, 48, text_win[6], 8);
}
